package it.rikesh.aip2011.dataloading;

import it.rikesh.aip2011.shared.model.Home;

import java.io.InputStream;
import java.util.Stack;
import java.util.logging.Logger;

import javax.servlet.ServletContext;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.slim3.datastore.Datastore;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.Text;

/**
 * @author  RN
 */
public class HomeHandler extends DefaultHandler{
    private static final Logger log = Logger.getLogger(HomeHandler.class.getName());
    private Stack<Home> homeStack;
    private String homeCharacters;
    
    public HomeHandler(ServletContext context){
        SAXParserFactory hutFactory = SAXParserFactory.newInstance();
        try {
            SAXParser hutSaxParser = hutFactory.newSAXParser();
            InputStream is = context.getResourceAsStream("/data/home.xml");
            hutSaxParser.parse(is, this);
        } catch (Throwable t) {
            log.severe("Errore nel caricamento dati da xml: "+t);
            t.printStackTrace();

        }
    }
    
    public void startDocument() 
    throws SAXException {

        homeStack = new Stack<Home>();

    }

    public void startElement(String namespaceURI, 
            String localName,
            String qualifiedName,
            Attributes attributes) 
    throws SAXException {

        if (qualifiedName.equals("home")) {

            Home home = new Home();
            Key key = Datastore.createKey(Home.class, Datastore.query(Home.class).count()+1);
            home.setKey(key);
            homeStack.push(home);

        }

    }

    public void endElement(String namespaceURI, 
            String simpleName,
            String qualifiedName) 
    throws SAXException {

        if (!homeStack.isEmpty()) {

            if (qualifiedName.equals("home")) {

                Datastore.put(homeStack.pop());


            } 
            else if (qualifiedName.equals("type")) {

                Home home = homeStack.pop();
                home.setType(homeCharacters);
                homeStack.push(home);

            } 
            else if (qualifiedName.equals("title")) {

                Home home = homeStack.pop();
                home.setTitle(homeCharacters);
                homeStack.push(home);

            } 
            else if (qualifiedName.equals("subtitle")) {

                Home home = homeStack.pop();
                home.setSubtitle(homeCharacters);
                homeStack.push(home);

            } 
            else if (qualifiedName.equals("description")) {

                Home home = homeStack.pop();
                home.setDescription(new Text(homeCharacters));
                homeStack.push(home);

            } 
            else if (qualifiedName.equals("img")) {

                Home home = homeStack.pop();
                home.setImg(homeCharacters);
                homeStack.push(home);

            }
        }

    }

    public void characters(char buf[], int offset, int len) 
    throws SAXException {

        homeCharacters = new String(buf, offset, len);

    }
}
